<?php
include '../config.php';
include '../classes/BadgeClass.php';
include 'utils.php';

session_start();

$location = "Location: " . $ISSUER_HOST . "/create_badge_result.php";

// Connecting, selecting database
$mysqli = new mysqli($MYSQL_HOST, $MYSQL_USERNAME, $MYSQL_PASSWORD, $MYSQL_DB_NAME);
if ($mysqli->connect_errno) {
	$_SESSION[$CB_MSG_TAG] = $DB_CONN_ERR;
	$_SESSION[$DB_CONN_ERRNO] = $mysqli->connect_errno;
	$_SESSION[$DB_CONN_ERROR] = $mysqli->connect_error;
	header($location);
	exit();
}

$mysqli->set_charset("utf8");

$name = $_SESSION["nomeBadge"];

$nameForFile = str_replace(' ', '_', trim($name));
$nameForQuery = prepareString($mysqli, $_SESSION["nomeBadge"]);
$description = prepareString($mysqli, $_SESSION["descrizioneBadge"]);
$tags = prepareString($mysqli, $_SESSION["tagsBadge"]);

if (isset($_SESSION["condivisaBadge"])) {
	$issuersQuery = 'SELECT `issuerId` FROM `issuer`';
	$issuers = $mysqli->query($issuersQuery);
	while ($issuerRow = $issuers->fetch_assoc()) {
		// Create new badge json file
		$file_name = "badge_" . $nameForFile . "_"  . $issuerRow['issuerId'] . ".json";
		
		$badge_asser = new BadgeClass();
		$badge_asser->setName($name);
		$badge_asser->setDescription($description);
		$badge_asser->setImage($ISSUER_HOST . "/img/badges_png/" . $nameForFile . ".png");
		$badge_asser->setCriteria($ISSUER_HOST . "/badges_page.php");
		$badge_asser->setIssuer($JSON_FILES_URL . "/issuer_" . $issuerRow['issuerId'] . ".json");
		$badge_asser->setTags(preg_split("/ /", $tags));
		
		$json_string = json_encode($badge_asser->getArray());
		
		createFile($JSON_BADGES_PATH_FROM_PHP, $file_name, $json_string);
	}
} else {
	// Create new badge json file
	$file_name = "badge_" . $nameForFile . ".json";
	
	$badge_asser = new BadgeClass();
	$badge_asser->setName($name);
	$badge_asser->setDescription($description);
	$badge_asser->setImage($ISSUER_HOST . "/img/badges_png/" . $nameForFile . ".png");
	$badge_asser->setCriteria($ISSUER_HOST . "/badges_page.php");
	$badge_asser->setIssuer($JSON_FILES_URL . "/issuer_" . $_SESSION['id_issuer'] . ".json");
	$badge_asser->setTags(preg_split("/ /", $tags));
	
	$json_string = json_encode($badge_asser->getArray());
	
	$created = createFile($JSON_BADGES_PATH_FROM_PHP, $file_name, $json_string);
	
	if (!$created) {
	 	unlink($BADGE_IMG_PATH_FROM_PHP . "/" . $nameForFile . ".png");
	 	unlink($JSON_BADGES_PATH_FROM_PHP . "/" . $file_name);
	 	$mysqli->close();
	 	$_SESSION[$CB_MSG_TAG] = $FILE_ERR;
	 	header($location);
	 	exit();
	}
}

$image = "img/badges_png/" . $nameForFile . ".png";
if (isset($_SESSION["condivisaBadge"]))
 	$condivisa = TRUE;
else
 	$condivisa = FALSE;

$issuer = $_SESSION["id_issuer"];
$categoria = $_SESSION["categoriaBadge"];

// Performing SQL query
$query = sprintf($INSERT_BADGE, $nameForQuery, $description, $image, $tags, $issuer, $condivisa, $categoria);

$result = $mysqli->query($query);

if (!$result) {
 	unlink($BADGE_IMG_PATH_FROM_PHP . "/" . $nameForFile . ".png");
 	unlink($JSON_BADGES_PATH_FROM_PHP . "/" . $file_name);
 	mysqli_free_result($result);
 	$mysqli->close();
 	$_SESSION[$CB_MSG_TAG] = $QUERY_ERR;
 	$_SESSION[$QUERY_TAG] = $query;
	$_SESSION[$QUERY_ERRNO] = $mysqli->errno;
 	$_SESSION[$QUERY_ERROR] = $mysqli->error;
 	header($location);
 	exit();
}

mysqli_free_result($result);
 
if ($_SESSION['criteria']) {
 	$query_badge = "SELECT badgeId FROM badge WHERE nome='" . $nameForQuery . "'" ;
 	$result_id = $mysqli->query($query_badge);
 	$row = $result_id->fetch_assoc();
 	$id = $row["badgeId"];
 	$num = count($_POST["tipo"]);
 	for ($i = 0; $i < $num; $i++) {
 		$tipo = $_POST["tipo"][$i];
 		$contenuto = $_POST["contenuto"][$i];
		echo $tipo . " " . $contenuto . "<br>";
 		$insert_query = sprintf($INSERT_CRITERIA, $id, $tipo, $contenuto);
 		$result_criteria = $mysqli->query($insert_query);
 	}
 	mysqli_free_result($result_criteria);
 	
}
// Closing connection
mysqli_free_result($result_id);
$mysqli->close();

$_SESSION[$CB_MSG_TAG] = $MSG_OK;
header($location);
exit();
?>
